package com.winhxd.b2c.admin.module.customer.controller;

import com.winhxd.b2c.admin.common.context.UserManager;
import com.winhxd.b2c.admin.common.security.annotation.CheckPermission;
import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.customer.condition.CopyPageConfigCondition;
import com.winhxd.b2c.common.domain.customer.condition.CustomerPageConfigCondition;
import com.winhxd.b2c.common.domain.customer.condition.QueryPageConfigCondition;
import com.winhxd.b2c.common.domain.customer.model.CustomerPageConfig;
import com.winhxd.b2c.common.domain.product.condition.ProductCondition;
import com.winhxd.b2c.common.domain.product.vo.ProductCategoryVO;
import com.winhxd.b2c.common.domain.product.vo.ProductListForAssemblyWebVO;
import com.winhxd.b2c.common.domain.system.security.enums.PermissionEnum;
import com.winhxd.b2c.common.domain.system.user.vo.UserInfo;
import com.winhxd.b2c.common.feign.customer.PageConfigServiceClient;
import com.winhxd.b2c.common.feign.product.ProductCategoryServiceClient;
import com.winhxd.b2c.common.feign.product.ProductServiceClient;
import com.winhxd.b2c.common.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @program: hxd-outlet-parent
 * @description: 后台页面配置接口
 * @author: ChenYanqi
 * @create: 2019-01-11 18:01
 **/
@Api(value = "后台页面配置接口", tags = "后台页面配置接口")
@RestController
@RequestMapping(value = "customer/pageConfig")
public class PageConfigController {

    private Logger logger = LoggerFactory.getLogger(PageConfigController.class);

    @Autowired
    PageConfigServiceClient pageConfigServiceClient;

    @Autowired
    private ProductCategoryServiceClient productCategoryServiceClient;

    @Autowired
    private ProductServiceClient productServiceClient;

    @CheckPermission(PermissionEnum.CUSTOMER_PAGE_MANAGEMENT_ADD)
    @ApiOperation(value = "新增自定义页面", response = Integer.class, notes = "新增自定义页面")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/savePageConfig")
    public ResponseResult<Boolean> savePageConfig(@RequestBody CustomerPageConfigCondition condition) {
        logger.info("新增自定义页面开始，入参为 -{}", JsonUtil.toJSONString(condition));
        UserInfo currentUser = UserManager.getCurrentUser();
        condition.setLevel(currentUser.getLevel());
        condition.setCreatedBy(currentUser.getId());
        ResponseResult<Boolean> result = pageConfigServiceClient.savePageConfig(condition);
        return result;
    }

    @CheckPermission(PermissionEnum.CUSTOMER_PAGE_MANAGEMENT_EDIT)
    @ApiOperation(value = "修改自定义页面", response = Integer.class, notes = "修改自定义页面")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/modifyPageConfig")
    public ResponseResult<Boolean> modifyPageConfig(@RequestBody CustomerPageConfigCondition condition) {
        logger.info("修改自定义页面开始，入参为 -{}", JsonUtil.toJSONString(condition));
        UserInfo currentUser = UserManager.getCurrentUser();
        condition.setLevel(currentUser.getLevel());
        condition.setCreatedBy(currentUser.getId());
        ResponseResult<Boolean> result = pageConfigServiceClient.modifyPageConfig(condition);
        return result;
    }


    @CheckPermission(PermissionEnum.CUSTOMER_PAGE_MANAGEMENT_DELETE)
    @ApiOperation(value = "删除自定义页面", response = Integer.class, notes = "删除自定义页面")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/deletePageConfig")
    public ResponseResult<Boolean> deletePageConfig(@RequestBody QueryPageConfigCondition condition) {
        logger.info("删除自定义页面开始，入参为 -{}", JsonUtil.toJSONString(condition));
        ResponseResult<Boolean> result = pageConfigServiceClient.deletePageConfig(condition);
        return result;
    }

    @CheckPermission(PermissionEnum.CUSTOMER_PAGE_MANAGEMENT_LIST)
    @ApiOperation(value = "查询自定义页面列表", response = Integer.class, notes = "查询自定义页面列表")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/queryPageConfig")
    public ResponseResult<PagedList<CustomerPageConfig>> queryPageConfig(@RequestBody QueryPageConfigCondition condition) {
        logger.info("查询自定义页面开始，入参为 -{}", JsonUtil.toJSONString(condition));
        UserInfo currentUser = UserManager.getCurrentUser();
        condition.setLevel(currentUser.getLevel());
        condition.setSysUserId(currentUser.getId());
        ResponseResult<PagedList<CustomerPageConfig>> result = pageConfigServiceClient.queryPageConfig(condition);
        return result;
    }

    @CheckPermission(PermissionEnum.CUSTOMER_PAGE_MANAGEMENT_RELEASE)
    @ApiOperation(value = "发布页面", response = Integer.class, notes = "发布页面")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/releasePageConfig")
    public ResponseResult<Boolean> releasePageConfig(@RequestBody QueryPageConfigCondition condition) {
        logger.info("发布页面开始，入参为 -{}", JsonUtil.toJSONString(condition));
        UserInfo currentUser = UserManager.getCurrentUser();
        condition.setLevel(currentUser.getLevel());
        condition.setSysUserId(currentUser.getId());
        ResponseResult<Boolean> result = pageConfigServiceClient.releasePageConfig(condition);
        return result;
    }

    @CheckPermission(PermissionEnum.CUSTOMER_PAGE_MANAGEMENT_SET_HOMEPAGE)
    @ApiOperation(value = "设置主页", response = Integer.class, notes = "设置主页")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/setHomePage")
    public ResponseResult<Boolean> setHomePage(@RequestBody QueryPageConfigCondition condition) {
        logger.info("设置主页开始，入参为 -{}", JsonUtil.toJSONString(condition));
        UserInfo currentUser = UserManager.getCurrentUser();
        condition.setLevel(currentUser.getLevel());
        condition.setSysUserId(currentUser.getId());
        ResponseResult<Boolean> result = pageConfigServiceClient.setHomePage(condition);
        return result;
    }

    @CheckPermission(PermissionEnum.CUSTOMER_PAGE_MANAGEMENT_COPY)
    @ApiOperation(value = "复制页面", response = Integer.class, notes = "复制页面")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/copyPageConfig")
    public ResponseResult<Boolean> copyPageConfig(@RequestBody CopyPageConfigCondition condition) {
        logger.info("复制页面开始，入参为 -{}", JsonUtil.toJSONString(condition));
        UserInfo currentUser = UserManager.getCurrentUser();
        condition.setLevel(currentUser.getLevel());
        condition.setSysUserId(currentUser.getId());
        ResponseResult<Boolean> result = pageConfigServiceClient.copyPageConfig(condition);
        return result;
    }

    @CheckPermission(PermissionEnum.CUSTOMER_PAGE_MANAGEMENT_EDIT)
    @ApiOperation(value = "根据id获取页面详情", response = Integer.class, notes = "根据id获取页面详情")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    @PostMapping(value = "/getPageConfigById")
    public ResponseResult<CustomerPageConfig> getPageConfigById(@RequestParam("id") Long id) {
        logger.info("根据id获取页面详情开始，入参为 -{}", id);
        ResponseResult<CustomerPageConfig> result = pageConfigServiceClient.getPageConfigById(id);
        return result;
    }

    @CheckPermission({PermissionEnum.CUSTOMER_PAGE_MANAGEMENT})
    @ApiOperation(value = "查询所有商品分类")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @RequestMapping(value = "/category/findProductCategory", method = RequestMethod.POST)
    public ResponseResult<List<ProductCategoryVO>> findProductCategory(){
        ResponseResult<List<ProductCategoryVO>> result = productCategoryServiceClient.findProductCategory();
        return result;
    }

    @CheckPermission({PermissionEnum.CUSTOMER_PAGE_MANAGEMENT})
    @ApiOperation(value = "Web端-获取商品列表(商城组件，带图片)", notes = "Web端-获取商品列表(商城组件，带图片)")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @PostMapping(value = "/product/findProductListForAssemblyByPage")
    public ResponseResult<PagedList<ProductListForAssemblyWebVO>> findProductListForAssemblyByPage(@RequestBody ProductCondition condition) {
        UserInfo userInfo = UserManager.getCurrentUser();
        condition.setCustomerId(userInfo.getCustomerId());
        ResponseResult<PagedList<ProductListForAssemblyWebVO>> responseResult = productServiceClient.findProductListForAssemblyByPage(condition);
        return responseResult;
    }

}
